class NameAliasesController < ApplicationController
  filter_access_to :all

  def create
    @artist = Artist.find(params[:name_alias][:id])
    name_alias = NameAlias.new(:name => params[:name_alias][:alias_name])
    name_alias.artist_id = @artist.id

    respond_to do |format|
      if name_alias.save
        format.html {redirect_to_request_uri}
        format.js {
          render :update do |page|
            page.replace_html("aliases", :partial => "artists/aliases",
                                         :locals => {:artist => @artist } )
          end
        }
      else
        format.html {redirect_to_request_uri}
        format.js {
          render :update do |page|
            page.replace_html(:errors, name_alias) 
          end
        }
      end
    end
  end

  def edit
    @name_alias = NameAlias.find(params[:id])
  end

  def update
    @name_alias = NameAlias.find(params[:id])

    respond_to do |format|
      if @name_alias.update_attributes(params[:name_alias])
        format.html {redirect_to_request_uri}
        #in place edit for the javascript.
      end
    end
  end

  def destroy
    bad_alias = NameAlias.find(params[:id])
    @artist = Artist.find(bad_alias.artist_id)

    bad_alias.delete

    entry = Hash.new
    entry.store("deleted alias", [@artist.name])
    Edit.create_edit(entry, @artist)

    respond_to do |format|
      format.html {redirect_to_request_uri}
      format.js {
        render :update do |page|
          page.replace_html("aliases", :partial => "artists/aliases",
                                       :locals => {:artist => @artist } )
        end
      }
    end
  end

end
